package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.PreviewActivity;
import flc.ast.adapter.TemplateAdapter;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.FragmentTemplateBinding;
import flc.ast.utils.OkHttpPostUtil;
import flc.ast.utils.ResumeDataUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.view.container.StkLinearLayout;
import stark.common.bean.StkResumeTemplate;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TemplateFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TemplateFragment extends BaseNoModelFragment<FragmentTemplateBinding> {
    private List<ResumeBean> listShow = new ArrayList<>();
    private TemplateAdapter templateAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_template;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.rvTemplateList.setLayoutManager(new GridLayoutManager(mContext, 2));
        templateAdapter = new TemplateAdapter();
        mDataBinding.rvTemplateList.setAdapter(templateAdapter);
        templateAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (listShow.size() == 0) {
            ToastUtils.showShort(getString(R.string.please_create_resume));
        } else {
            ResumeBean resumeBean = listShow.get(0);
            resumeBean.setTemplateName(templateAdapter.getItem(position).name);
            generate();
        }
    }

    @Override
    protected void initData() {
        getTemplateData();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getData();
        }
    }

    private void getData() {
        listShow.clear();
        List<ResumeBean> listSp = ResumeDataUtils.getResumeList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
        }
    }

    private void getTemplateData() {
        StkResApi.getResumeTemplateList(this, new IReqRetCallback<List<StkResumeTemplate>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResumeTemplate> result) {
                if (result != null && result.size() > 0) {
                    templateAdapter.setList(result);
                }
            }
        });
    }

    private void generate() {
        for (ResumeBean.Module resumeDatum : listShow.get(0).getResumeData()) {
            if (resumeDatum.isShow()) {
                if (resumeDatum.getData() == null) {
                    ToastUtils.showShort(getString(R.string.please_input_resume_data));
                    return;
                }
            }
        }
        OkHttpPostUtil.postJson(AppConfig.RESUME_GENERATE_DATA, new Gson().toJson(listShow.get(0)), new OkHttpPostUtil.PostCallback() {
            @Override
            public void onSuccess(String response) {
                PreviewActivity.response = response;
                startActivity(PreviewActivity.class);
            }

            @Override
            public void onFailure(Exception e) {
                ToastUtils.showShort(e.getMessage());
            }
        });
    }

}